package com.eshopkeeper.provider.sys;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.eshopkeeper.core.base.BaseProviderImpl;
import com.eshopkeeper.dao.sys.SysMenuMapper;
import com.eshopkeeper.model.sys.SysMenu;
import com.eshopkeeper.model.sys.ext.SysMenuBean;
import com.weibo.api.motan.config.springsupport.annotation.MotanService;

/**
 * @author chenshufeng
 * @version 2017年3月17日20日 下午3:19:19
 */
//@CacheConfig(cacheNames = "sysMenu")
@MotanService(basicService = "serviceBasicConfig")
public class SysMenuProviderImpl extends BaseProviderImpl<SysMenu> implements ISysMenuProvider {
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private ISysDicProvider sysDicProvider;

    public Page<SysMenu> query(Map<String, Object> params) {
        Page<Long> idPage = this.getPage(params);
        idPage.setRecords(mapper.selectIdByMap(idPage, params));
        Page<SysMenu> pageInfo = getPage(idPage);
        Map<String, String> menuTypeMap = sysDicProvider.queryDicByDicIndexKey("MENUTYPE");
        for (SysMenu sysMenu : pageInfo.getRecords()) {
            if (sysMenu.getMenuType() != null) {
                sysMenu.setRemark(menuTypeMap.get(sysMenu.getMenuType().toString()));
            }
        }
        return pageInfo;
    }

    /* (non-Javadoc)
     * @see com.eshopkeeper.provider.SysMenuProvider#getPermissions() */
    @Override
    public List<Map<String, String>> getPermissions() {
        return sysMenuMapper.getPermissions();
    }

	@Override
	public void insertSysMenu(SysMenu sysMenu) {
		
		sysMenuMapper.insertSysMenu(sysMenu);
	}
	
	@Override
	public List<Map<String, Object>> queryParentList() {
		return sysMenuMapper.queryParentList();
	}

	@Override
	public Page<SysMenuBean> queryMenuList(Map<String, Object> mapParam) {
        mapParam.put("pageSize","50");
		Page<Long> idPage = this.getPage(mapParam);
        List<Long> ids = mapper.selectIdByMap(idPage, mapParam);
        Page<SysMenuBean> pageInfo = new Page<SysMenuBean>(idPage.getCurrent(), idPage.getSize());
        pageInfo.setTotal(idPage.getTotal());
        if (ids.size() > 0) {
        	List<SysMenuBean> sysMenuList = sysMenuMapper.queryMenuList(ids);
        	pageInfo.setRecords(sysMenuList);
        } else {
        	pageInfo.setRecords(null);
        }
        
		return pageInfo;
	}

    /**
     * 菜单排序编辑
     * @param sysMenuBean
     * @return
     */
    @Transactional
    @Override
    public int updateMenuById(SysMenuBean sysMenuBean) {
        return  sysMenuMapper.updateMenuById(sysMenuBean);
    }

}
